package com.zl.tby.controller;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyMountainPeakQuery;
import com.zl.tby.model.TbyMountainPeak;
import com.zl.tby.model.TbySpeciesCategory;
import com.zl.tby.service.ITbySpeciesCategoryService;
import com.zl.tby.vo.TbySpeciesCategoryBean;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 山峰信息控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/species_category")
public class TbySpeciesCategoryController {

    @Inject
    private ITbySpeciesCategoryService tbySpeciesCategoryService;

    /**
     * 保存或新增面积
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbySpeciesCategory> saveOrUpdate(@Validated TbySpeciesCategory entity) {
        try {
            if (entity.getId() == null) {
                return tbySpeciesCategoryService.add(entity);
            }
            return tbySpeciesCategoryService.update(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbySpeciesCategory> get(@NotNull Long id) {
        TbySpeciesCategory entity = this.tbySpeciesCategoryService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbySpeciesCategoryService.delete(id);
    }

    /**
     * 树形表格数据
     * @return 分页结果
     */
    @Mapping(value = "/tree_table_list", method = MethodType.GET)
    public R<TbyPageResult<TbySpeciesCategoryBean>> treeTableList() {
        return R.ok(this.tbySpeciesCategoryService.treeTableList());
    }

    /**
     * 下拉选数据
     * @return 结果
     */
    @Mapping(value = "/options", method = MethodType.GET)
    public R<List<OptionType>> options() {
        return R.ok(this.tbySpeciesCategoryService.options());
    }

}
